package com.tubi.file;

import java.io.File;
import java.util.HashMap;

public class filedemo5 {
    public static void main(String[] args) {
        //统计文件出现的次数
        File file = new File("test-file");
         //创建hashMap 存放键值对
        HashMap<String, Integer> hm = new HashMap<>();

        getCount(hm,file);
        System.out.println(hm);
    }

    private static void getCount(HashMap<String, Integer> hm, File file) {
        File[] files = file.listFiles();
        for (File f : files) {
            if(f.isFile()){
                String filename = f.getName();//获取文件名字
                String[] fileNameArr = filename.split("\\."); //截取后缀
                if (fileNameArr.length == 2){
                    String fileEndName = fileNameArr[1];
                    if(hm.containsKey(fileEndName)){//判断集合内是否有这个key
                        Integer count = hm.get(fileEndName); //获取key的值
                        count++;
                        hm.put(fileEndName, count);
                    }else {
                        hm.put(fileEndName, 1);
                    }
                }
            }else {
                getCount(hm, f);
            }
            
        }
    }
}
